//
//  RecommendCycleView.m
//  sanguosha
//
//  Created by caiqilin on 2017/7/3.
//  Copyright © 2017年 caiqilin. All rights reserved.
//

#import "RecommendCycleView.h"
#import "CollectionCycleCell.h"
#import "UIImageView+WebCache.h"
#import "SVProgressHUD.h"
#import "MPWebViewController.h"
@interface RecommendCycleView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (nonatomic, strong) NSTimer *timer;
@end
@implementation RecommendCycleView
+ (instancetype)recommendCycleView {
    return [[[NSBundle mainBundle] loadNibNamed:@"RecommendCycleView" owner:nil options:nil] firstObject];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    [_collectionView registerNib:[UINib nibWithNibName:@"CollectionCycleCell" bundle:nil] forCellWithReuseIdentifier:@"kCycleCellID"];
    _collectionView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_homebanner.jpg"]];
    
    [self requestBanner];
}

- (void)requestBanner {
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"banner"];
    //查找GameScore表的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        self.cycleArr = array;
        _pageControl.numberOfPages = array.count;
        [self.collectionView reloadData];
        [SVProgressHUD dismiss];
        [self stopTimer];
        [self startTimer];
    }];
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //    layout.itemSize = _collectionView.bounds.size;
    layout.itemSize = _collectionView.bounds.size;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _cycleArr.count * 10000;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionCycleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kCycleCellID" forIndexPath:indexPath];
    BmobObject *obj = _cycleArr[indexPath.item % _cycleArr.count];
    NSString *title = [obj objectForKey:@"title"];
    NSString *href =  [obj objectForKey:@"href"];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:href] placeholderImage:[UIImage imageNamed:@"icon_homebanner.jpg"]];
    cell.bottomLabel.text = title;
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(SCREEN_WIDTH, SCREEN_WIDTH * 4 / 8);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 2.计算pageControl的currentIndex
    _pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5) % (_cycleArr.count > 0 ? _cycleArr.count : 1);
    
//    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

#pragma mark - 定时器控制
- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)nextPage
{
    
    // 1.获取滚动的偏移量
    CGFloat currentOffsetX = _collectionView.contentOffset.x;
    CGFloat offsetX = currentOffsetX + _collectionView.ex_width;
    
    // 2.滚动该位置
//    collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    
    
    //    // 1.获取滚动的偏移量
    //    let currentOffsetX = collectionView.contentOffset.x
    //    let offsetX = currentOffsetX + collectionView.bounds.width
    //
    //    // 2.滚动该位置
    //    collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    
    
//    NSInteger page = self.pageControl.currentPage + 1;
//    if (page == self.pageControl.numberOfPages) {
//        page = 0;
//    }
    
//    CGPoint offset = self.collectionView.contentOffset;
//    offset.x = page * self.collectionView.frame.size.width;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BmobObject *obj = _cycleArr[indexPath.item % _cycleArr.count];
//    NSString *pid = [NSString stringWithFormat:@"%ld",indexPath.item + 1];
    if ([_delegate respondsToSelector:@selector(recommendCycleViewClick::)]) {
        [_delegate recommendCycleViewClick:self :obj];
    }
    
}
@end
